use "Dataset for Publication main.dta", clear
xtset subject period

/// global including individual risk, social preferences and demographic variables.
global controlsindiv age male riskaversion preferencesefficiency /// 
inequalityaversion inequalityloving spiteful swiss onlychild educationparents ///
numberknown bigtown unil bachelor grade 

/// global including team-level risk, social preferences and demographic variables

global controlsteams ageteammean malemale malefemale riskaversionteammean preferencesefficiencyteam /// 
inequalityaversionteam inequalitylovingteam spitefulteam swissswiss swissother onlychildteam educationparentsteam ///
numberknownteam bigtownteam unilteam bachelorteam gradeteam 

/// global including all periods except the first period since it is the dummy period.

global period period2 period3 period4 period5 period6 period7 period8 


//Figure 2
reg estimatedabilityde ownabilityde if easy == 1 & period == 1
reg estimatedabilityde ownabilityde if easy == 0 & period == 1 
display (.3188811-.2160839)/sqrt(.0605454^2 + .0485923^2) // t-stat joint test
display (1-t(238,1.3241349))*2 // p-value joint test 
g flinehard = -.4833333 + .3188811*ownabilityde 
g flineeasy =   1.2     + .2160839*ownabilityde
twoway (scatter estimatedabilityde ownabilityde [fweight=subject] if period == 1 & easy == 0, msymbol(o)) (line flinehard ownabilityde if period == 1 & easy == 0, msymbol(o)), title(HARD) xlabel(-5(5)5) ylabel(-6(2)6) xtitle(true rank - mean of true rank) ytitle(prior belief about rank - mean of true rank) aspectratio(1.2) leg(off) ///
graphregion(color(white)) xline(0, lcolor(gray))
twoway (scatter estimatedabilityde ownabilityde [fweight=subject] if period == 1 & easy == 1, msymbol(o)) (line flineeasy ownabilityde if period == 1 & easy == 1, msymbol(o)), title(EASY) xlabel(-5(5)5) ylabel(-6(2)6) xtitle(true rank - mean of true rank) ytitle(prior belief about rank - mean of true rank) aspectratio(1.2) leg(off) ///
graphregion(color(white)) xline(0, lcolor(gray))

//Figure 3
preserve
collapse (mean) clicks (semean) clickssemean=clicks, by(easy period)
g rcapmax = clicks + clickssemean
g rcapmin = clicks - clickssemean
twoway (connected clicks period if easy == 1, lcolor(red)) (connected clicks period if easy == 0, lcolor(blue)) (rcap rcapmax rcapmin period), xlabel(1(1)8) ylabel(18(2)28) leg(off)graphregion(color(white)) xtitle(period) ytitle(average number of clicks) yline(24.38, lcolor(red) lpattern(dash)) yline(20.12, lcolor(blue) lpattern(dash))
restore

//Figure 4
preserve
collapse (mean) catches (semean) catchessemean=catches, by(easy period)
g rcapmax = catches + catchessemean
g rcapmin = catches - catchessemean
twoway (connected catches period if easy == 1, lcolor(blue)) (connected catches period if easy == 0, lcolor(red)) (rcap rcapmax rcapmin period), xlabel(1(1)8) leg(off) graphregion(color(white)) xtitle(period) ytitle(average number of catches) yline(34.17, lcolor(blue) lpattern(dash)) yline(32.61, lcolor(red) lpattern(dash))
graph save catches.gph, replace
restore

preserve
collapse (mean) teamrevenue (semean) teamrevenuesemean=teamrevenue, by(easy period)
g rcapmax = teamrevenue + teamrevenuesemean
g rcapmin = teamrevenue - teamrevenuesemean
twoway (connected teamrevenue period if easy == 1, lcolor(blue)) (connected teamrevenue period if easy == 0, lcolor(red)) (rcap rcapmax rcapmin period), xlabel(1(1)8) leg(off) graphregion(color(white)) xtitle(period) ytitle(average team revenue) yline(49.78, lcolor(blue) lpattern(dash)) yline(46.78, lcolor(red) lpattern(dash))
graph save revenue.gph, replace
restore

preserve
collapse (mean) payoffs (semean) payoffssemean=payoffs, by(easy period)
g rcapmax = payoffs + payoffssemean
g rcapmin = payoffs - payoffssemean
twoway (connected payoffs period if easy == 1, lcolor(blue)) (connected payoffs period if easy == 0, lcolor(red)) (rcap rcapmax rcapmin period), xlabel(1(1)8) leg(off) graphregion(color(white)) xtitle(period) ytitle(average individual payoff) yline(20.83, lcolor(blue) lpattern(dash)) yline(20.03, lcolor(red) lpattern(dash))
graph save payoffs.gph, replace
restore

graph combine "catches.gph" "revenue.gph" "payoffs.gph", graphregion(color(white))

//Table 2
xtreg clicks easy, cluster(pair)
xtreg clicks easy ownability partnerability male, cluster(pair)
xtreg clicks easy ownability partnerability male estimatedability partnerestimatedability, cluster(pair)

//Table 3/9

xtreg clicks ownability ownbias partnerability partnerbias, cluster(pair)
xtreg clicks ownability ownbias partnerability partnerbias $controlsindiv $period, cluster(pair)

preserve
replace subject = subject/2
keep if subject == int(subject)
xtreg teamrevenue abilitysubjecti biassubjecti abilitysubjectj biassubjectj, cluster(pair)
xtreg teamrevenue abilitysubjecti biassubjecti abilitysubjectj biassubjectj $controlsteams $period, cluster(pair)
restore
//Table 4/10 

xtreg payoffs ownability ownbias ownbiassquared partnerability partnerbias, cluster(pair)
test ownbias ownbiassquared
xtreg payoffs ownability ownbias ownbiassquared partnerability partnerbias $controlsindiv $period, cluster(pair)
test ownbias ownbiassquared

preserve
replace subject = subject/2
keep if subject == int(subject)
xtreg teampayoffs abilitysubjecti biassubjecti abilitysubjectj biassubjectj, cluster(pair)
xtreg teampayoffs abilitysubjecti biassubjecti abilitysubjectj biassubjectj $controlsteams $period, cluster(pair)
restore


//Figure 5
g flinenocontrols = (.0109593*ownbias -.0000875*ownbiassquared)
g flinewcontrols =  (.0089289*ownbias -.0000745*ownbiassquared)
twoway (line flinewcontrols ownbias if period == 1, sort msymbol(o) lpattern(solid) color(black)) (line flinenocontrols ownbias if period == 1, sort msymbol(o) lpattern(dash) color(black)) ///
, ylabel(-4(1)1) xlabel(-200(40)200)  xtitle(self-confidence bias) ytitle(estimated effect of self-confidence bias on payoff gap) ///
graphregion(color(white))  yline(0, lcolor(gray))




//Figure 6
reg estimatedability2de ownabilityde if easy == 1 & period == 1
reg estimatedability2de ownabilityde if easy == 0 & period == 1 
display (.4597902-.2342657)/sqrt(.0501206^2 + .0560764^2) // t-stat
display (1-t(238,2.9985726))*2 // p-value
g flinehard2 = -.525     + .4597902*ownabilityde 
g flineeasy2 = 1.283333  + .2342657*ownabilityde
twoway (scatter estimatedability2de ownabilityde [fweight=subject] if period == 1 & easy == 0, msymbol(o)) (line flinehard2 ownabilityde if period == 1 & easy == 0, msymbol(o)), title(HARD) xlabel(-5(5)5) ylabel(-6(2)6) xtitle(true rank - mean of true rank) ytitle(posterior belief about rank - mean of true rank) aspectratio(1.2) leg(off) ///
graphregion(color(white)) xline(0, lcolor(gray))
twoway (scatter estimatedability2de ownabilityde [fweight=subject] if period == 1 & easy == 1, msymbol(o)) (line flineeasy2 ownabilityde if period == 1 & easy == 1, msymbol(o)), title(EASY) xlabel(-5(5)5) ylabel(-6(2)6) xtitle(true rank - mean of true rank) ytitle(posterior belief about rank - mean of true rank) aspectratio(1.2) leg(off) ///
graphregion(color(white)) xline(0, lcolor(gray))

//Table 5
xtreg clicks ownability ownbias period partnerability partnerabilityperiod partnerbias partnerbiasperiod, cluster(pair)
test period = partnerabilityperiod = partnerbiasperiod = 0
xtreg clicks ownability ownbias period partnerability partnerabilityperiod partnerbias partnerbiasperiod $controlsindiv, cluster(pair)
test period = partnerabilityperiod = partnerbiasperiod = 0

//Table 6
sum $controlsindiv if period == 1
sum $controlsindiv if easy == 1 & period == 1
sum $controlsindiv if easy == 0 & period == 1


ttest age if period == 1, by(easy)
ttest male if period == 1, by(easy)
ttest riskaversion if period == 1, by(easy)
ttest preferencesefficiency if period == 1, by(easy)
ttest inequalityaversion if period == 1, by(easy)
ttest inequalityloving if period == 1, by(easy)
ttest spiteful if period == 1, by(easy)
ttest swiss if period == 1, by(easy)
ttest onlychild if period == 1, by(easy)
ttest educationparents if period == 1, by(easy)
ttest numberknown if period == 1, by(easy)
ttest bigtown if period == 1, by(easy)
ttest unil  if period == 1, by(easy)
ttest bachelor if period == 1, by(easy)
ttest grade if period == 1, by(easy)

//Table 7

xtreg clicks easy, cluster(pair)
matrix list r(table)    
xtreg catches easy, cluster(pair)
matrix list r(table)
xtreg teamrevenue easy, cluster(pair)
matrix list r(table) 
xtreg payoffs easy, cluster (pair)
matrix list r(table) 

reg clicks easy, cluster(pair)
matrix list r(table)    
reg catches easy, cluster(pair)
matrix list r(table)
reg teamrevenue easy, cluster(pair)
matrix list r(table) 
reg payoffs easy, cluster (pair)
matrix list r(table) 

wyoung, cmd("reg clicks easy, cluster(pair)" "reg catches easy, cluster(pair)" "reg teamrevenue easy, cluster(pair)" "reg payoffs easy, cluster(pair)") cluster(pair) familyp(easy) bootstraps(9999) seed(1)



//Table 8

xtreg clicksquaredroot ownability ownbias partnerability partnerbias $controlsindiv $period, cluster(pair)



//Figure 7
preserve
collapse (mean) clickstrial (semean) clickstrialsemean=clicks, by(trialmultiplier)
g rcapmax = clickstrial + clickstrialsemean 
g rcapmin = clickstrial - clickstrialsemean 
twoway (bar clickstrial trialmultiplier if trialmultiplier == 40) (bar clickstrial trialmultiplier if trialmultiplier == 100) (bar clickstrial trialmultiplier if trialmultiplier == 160) ///
(bar clickstrial trialmultiplier if trialmultiplier == 220) (rcap rcapmax rcapmin trialmultiplier) 
restore
//Figure 8
kdensity clickstrial if trialmultiplier == 40
kdensity clickstrial if trialmultiplier == 100
kdensity clickstrial if trialmultiplier == 160
kdensity clickstrial if trialmultiplier == 220

//Figure 9
hist clicks if easy == 1, freq graphregion(color(white)) ylabel(0(50)150) ytitle("Absolute Frequency")
hist clicks if easy == 0, freq graphregion(color(white)) ylabel(0(50)150) ytitle("Absolute Frequency")
//Figure 10
hist catches if easy == 1, freq graphregion(color(white)) ylabel(0(50)150) ytitle("Absolute Frequency") bin(15)
hist catches if easy == 0, freq graphregion(color(white)) ylabel(0(50)150) ytitle("Absolute Frequency") bin(15)


//Table 12 first and second column
reg ownbias riskaversion if period == 1, cluster(pair)

reg ownbias riskaversion male age preferencesefficiency inequalityaversion inequalityloving spiteful swiss onlychild educationparents numberknown bigtown unil bachelor grade if period == 1, cluster(pair)

//Table 13
reg clicks ownability ownbias partnerability partnerbias if period == 1, cluster(pair)
reg clicks ownability ownbias partnerability partnerbias $controlsindiv if period == 1, cluster(pair)
preserve
replace subject = subject/2
keep if subject == int(subject)
reg teamrevenue abilitysubjecti biassubjecti abilitysubjectj biassubjectj if period == 1, cluster(pair)
reg teamrevenue abilitysubjecti biassubjecti abilitysubjectj biassubjectj $controlsteams if period == 1, cluster(pair)
restore

//Figure 11

forv i = 1/8{
quietly regress clicks ownability ownbias partnerability partnerbias $controlsindiv  if period ==`i'
 estimate store _`i'
 }
  
  
coefplot _1 || _2 || _3 || _4 || _5 || _6 || _7 || _8 ||, mlabel("p = " + string(@pval,"%6.4f")) keep(partnerability) yline(0) bycoefs byopts(yrescale) vertical graphregion(color(white)) xtitle("period") ytitle("coefficient of partner ability multiplier") 





